<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>游戏大厅 - 网络五子棋</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            overflow: hidden;
            backdrop-filter: blur(10px);
        }
        
        header {
            background: linear-gradient(45deg, #667eea, #764ba2);
            color: white;
            padding: 30px 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .logo h1 {
            font-size: 2.5rem;
            margin-bottom: 5px;
        }
        
        .logo p {
            opacity: 0.9;
            font-size: 1.1rem;
        }
        
        .user-info {
            text-align: right;
        }
        
        .user-info .welcome {
            font-size: 1.2rem;
            margin-bottom: 5px;
        }
        
        .user-info .stats {
            font-size: 0.9rem;
            opacity: 0.9;
        }
        
        .main-content {
            display: flex;
            min-height: 500px;
        }
        
        .sidebar {
            width: 300px;
            background: #f8f9fa;
            padding: 30px 20px;
            border-right: 1px solid #e1e1e1;
        }
        
        .menu-item {
            padding: 15px 20px;
            margin-bottom: 10px;
            border-radius: 10px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 500;
            color: #333;
        }
        
        .menu-item.active {
            background: #667eea;
            color: white;
        }
        
        .menu-item:hover:not(.active) {
            background: #e9ecef;
        }
        
        .content {
            flex: 1;
            padding: 40px;
        }
        
        .match-section {
            text-align: center;
            padding: 40px 20px;
        }
        
        .match-title {
            font-size: 2rem;
            color: #333;
            margin-bottom: 30px;
        }
        
        .match-btn {
            background: linear-gradient(45deg, #ff6b6b, #ee5a24);
            color: white;
            border: none;
            padding: 20px 60px;
            font-size: 1.5rem;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 6px 20px rgba(238, 90, 36, 0.4);
            font-weight: bold;
            letter-spacing: 2px;
            margin-bottom: 30px;
        }
        
        .match-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 25px rgba(238, 90, 36, 0.6);
        }
        
        .match-btn:active {
            transform: translateY(-1px);
        }
        
        .match-btn.matching {
            background: linear-gradient(45deg, #4ecdc4, #44a08d);
            animation: pulse 1.5s infinite;
        }
        
        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.05); }
            100% { transform: scale(1); }
        }
        
        .match-status {
            font-size: 1.2rem;
            color: #666;
            margin-top: 20px;
        }
        
        .user-stats {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            margin-top: 40px;
        }
        
        .stats-title {
            font-size: 1.5rem;
            color: #333;
            margin-bottom: 20px;
            text-align: center;
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 20px;
        }
        
        .stat-item {
            text-align: center;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 10px;
        }
        
        .stat-value {
            font-size: 2rem;
            font-weight: bold;
            color: #667eea;
            margin-bottom: 5px;
        }
        
        .stat-label {
            color: #666;
            font-size: 0.9rem;
        }
        
        .message {
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            text-align: center;
            display: none;
        }
        
        .message.error {
            background: #ffe6e6;
            color: #d63031;
            border: 1px solid #ff7675;
        }
        
        .message.success {
            background: #e6f7e6;
            color: #27ae60;
            border: 1px solid #2ecc71;
        }
        
        .logout-btn {
            background: #6c757d;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 8px;
            cursor: pointer;
            transition: background 0.3s ease;
            margin-top: 20px;
            width: 100%;
        }
        
        .logout-btn:hover {
            background: #5a6268;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <div class="logo">
                <h1>⚫ 网络五子棋 ⚪</h1>
                <p>传统智慧 · 在线竞技 · 实时对战</p>
            </div>
            <div class="user-info">
                <div class="welcome">欢迎，<span id="userName">玩家</span>！</div>
                <div class="stats">积分: <span id="userPoints">0</span> | 胜率: <span id="winRate">0%</span></div>
            </div>
        </header>
        
        <div class="main-content">
            <div class="sidebar">
                <div class="menu-item active">开始匹配</div>
                <div class="menu-item">历史战绩</div>
                <div class="menu-item">游戏规则</div>
                <div class="menu-item">设置</div>
                <button class="logout-btn" onclick="logout()">退出登录</button>
            </div>
            
            <div class="content">
                <div id="message" class="message"></div>
                
                <div class="match-section">
                    <h2 class="match-title">开始匹配对手</h2>
                    <button id="matchBtn" class="match-btn" onclick="toggleMatch()">开始匹配</button>
                    <div id="matchStatus" class="match-status">点击上方按钮开始匹配对手</div>
                </div>
                
                <div class="user-stats">
                    <h3 class="stats-title">我的数据</h3>
                    <div class="stats-grid">
                        <div class="stat-item">
                            <div class="stat-value" id="totalGames">0</div>
                            <div class="stat-label">总场次</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-value" id="winGames">0</div>
                            <div class="stat-label">胜场</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-value" id="points">0</div>
                            <div class="stat-label">积分</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-value" id="winRateValue">0%</div>
                            <div class="stat-label">胜率</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        let ws = null;
        let isMatching = false;

        // 页面加载时获取用户信息并建立WebSocket连接
        window.onload = function() {
            fetchUserInfo();
            connectWebSocket();
        };

        function fetchUserInfo() {
            fetch('/info')
            .then(response => response.json())
            .then(data => {
                if (data.result) {
                    document.getElementById('userName').textContent = data.name;
                    document.getElementById('userPoints').textContent = data.points;
                    document.getElementById('totalGames').textContent = data.total_count;
                    document.getElementById('winGames').textContent = data.win_count;
                    document.getElementById('points').textContent = data.points;
                    
                    const winRate = data.total_count > 0 ? 
                        Math.round((data.win_count / data.total_count) * 100) : 0;
                    document.getElementById('winRate').textContent = winRate + '%';
                    document.getElementById('winRateValue').textContent = winRate + '%';
                } else {
                    showMessage('获取用户信息失败: ' + data.reason, 'error');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('网络错误，请刷新页面重试', 'error');
            });
        }

        function connectWebSocket() {
            const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
            const wsUrl = `${protocol}//${window.location.host}/hall`;
            
            ws = new WebSocket(wsUrl);
            
            ws.onopen = function() {
                console.log('WebSocket连接已建立');
                showMessage('已连接到游戏大厅', 'success');
                setTimeout(() => {
                    document.getElementById('message').style.display = 'none';
                }, 3000);
            };
            
            ws.onmessage = function(event) {
                const data = JSON.parse(event.data);
                handleWebSocketMessage(data);
            };
            
            ws.onclose = function() {
                console.log('WebSocket连接已关闭');
                showMessage('连接已断开，正在重连...', 'error');
                // 尝试重连
                setTimeout(connectWebSocket, 3000);
            };
            
            ws.onerror = function(error) {
                console.error('WebSocket错误:', error);
                showMessage('连接错误，请检查网络', 'error');
            };
        }

        function handleWebSocketMessage(data) {
            if (data.op_type === 'hall_ready') {
                if (data.result) {
                    showMessage('游戏大厅连接成功', 'success');
                } else {
                    showMessage('连接失败: ' + data.reason, 'error');
                }
            } else if (data.op_type === 'match_success') {
                isMatching = false;
                updateMatchButton();
                showMessage('匹配成功！正在进入游戏房间...', 'success');
                // 跳转到游戏房间
                setTimeout(() => {
                    window.location.href = 'room.html';
                }, 1000);
            } else if (data.op_type === 'match_start') {
                if (data.result) {
                    showMessage('已开始匹配，请等待...', 'success');
                } else {
                    isMatching = false;
                    updateMatchButton();
                    showMessage('匹配失败: ' + data.reason, 'error');
                }
            } else if (data.op_type === 'match_stop') {
                if (data.result) {
                    showMessage('已取消匹配', 'success');
                } else {
                    showMessage('取消匹配失败: ' + data.reason, 'error');
                }
            }
        }

        function toggleMatch() {
            if (!isMatching) {
                // 开始匹配
                const message = {
                    op_type: 'match_start'
                };
                ws.send(JSON.stringify(message));
                isMatching = true;
                updateMatchButton();
                document.getElementById('matchStatus').textContent = '正在寻找对手...';
            } else {
                // 取消匹配
                const message = {
                    op_type: 'match_stop'
                };
                ws.send(JSON.stringify(message));
                isMatching = false;
                updateMatchButton();
                document.getElementById('matchStatus').textContent = '已取消匹配';
            }
        }

        function updateMatchButton() {
            const matchBtn = document.getElementById('matchBtn');
            if (isMatching) {
                matchBtn.textContent = '取消匹配';
                matchBtn.classList.add('matching');
            } else {
                matchBtn.textContent = '开始匹配';
                matchBtn.classList.remove('matching');
            }
        }

        function showMessage(text, type) {
            const messageDiv = document.getElementById('message');
            messageDiv.textContent = text;
            messageDiv.className = `message ${type}`;
            messageDiv.style.display = 'block';
        }

        function logout() {
            // 关闭WebSocket连接
            if (ws) {
                ws.close();
            }
            // 跳转到登录页面
            window.location.href = 'login.html';
        }
    </script>
</body>
</html>